<?php

namespace app\common\model\proxy;

use app\common\model\BaseModel;

class User extends BaseModel
{
    protected $name = 'proxy_user';
    protected $pk = 'proxy_id';

    /**
     * 关联应用表
     */
    public function app()
    {
        return $this->belongsTo('app\\common\\model\\app\\App', 'app_id', 'app_id');
    }

    public static function checkExist($value, $field = "user_name", $exclude_proxy_id = 0)
    {
        return !!static::withoutGlobalScope()
            ->where($field, '=', $value)
            ->where("proxy_id", '<>', $exclude_proxy_id)
            ->value('proxy_id');
    }

    /**
     * @param $proxy_id
     * @param $with
     * @return self
     */
    public static function detail($proxy_id, $with = [])
    {
        return (new static())->with($with)->find($proxy_id);
    }


    /**
     * 关联用户角色表表
     */
    public function role()
    {
        return $this->belongsToMany('app\\common\\model\\auth\\Role', 'app\\common\\model\\auth\\UserRole');
    }

    public function userRole()
    {
        return $this->hasMany('app\\common\\model\\proxy\\UserRole', 'proxy_id', 'proxy_id');
    }

    /**
     * 关联应用表
     */
    public function user()
    {
        return $this->belongsTo('app\\common\\model\\user\\User', 'user_id', 'user_id');
    }


}